package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IClientCrmFileService;
import com.mdd.admin.validate.ClientCrmFileCreateValidate;
import com.mdd.admin.validate.ClientCrmFileUpdateValidate;
import com.mdd.admin.validate.ClientCrmFileSearchValidate;
import com.mdd.admin.vo.ClientCrmFileListedVo;
import com.mdd.admin.vo.ClientCrmFileDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.ClientCrmFile;
import com.mdd.common.mapper.ClientCrmFileMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 客户文件实现类
 * @author LikeAdmin
 */
@Service
public class ClientCrmFileServiceImpl implements IClientCrmFileService {
        
    @Resource
    ClientCrmFileMapper clientCrmFileMapper;

    /**
     * 客户文件列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<ClientCrmFileListedVo>
     */
    @Override
    public PageResult<ClientCrmFileListedVo> list(PageValidate pageValidate, ClientCrmFileSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<ClientCrmFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        clientCrmFileMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:fileDescriptor@file_descriptor:str",
            "like:fileName@file_name:str",
            "like:accountName@account_name:str",
            "=:uploaderid@uploaderId:int",
            "between:uploadTime@upload_time:str",
        });

        IPage<ClientCrmFile> iPage = clientCrmFileMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<ClientCrmFileListedVo> list = new LinkedList<>();
        for(ClientCrmFile item : iPage.getRecords()) {
            ClientCrmFileListedVo vo = new ClientCrmFileListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(item.getCreateTime());
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 客户文件详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return ClientCrmFile
     */
    @Override
    public ClientCrmFileDetailVo detail(Integer id) {
        ClientCrmFile model = clientCrmFileMapper.selectOne(
                new QueryWrapper<ClientCrmFile>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        ClientCrmFileDetailVo vo = new ClientCrmFileDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 客户文件新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(ClientCrmFileCreateValidate createValidate) {
        ClientCrmFile model = new ClientCrmFile();
        clientCrmFileMapper.insert(model);
    }

    /**
     * 客户文件编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(ClientCrmFileUpdateValidate updateValidate) {
        ClientCrmFile model = clientCrmFileMapper.selectOne(
                new QueryWrapper<ClientCrmFile>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        clientCrmFileMapper.updateById(model);
    }

    /**
     * 客户文件删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        ClientCrmFile model = clientCrmFileMapper.selectOne(
                new QueryWrapper<ClientCrmFile>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        clientCrmFileMapper.delete(new QueryWrapper<ClientCrmFile>().eq("id", id));
    }

}
